<script setup lang="tsx">
import { PropType, reactive, watch, ref } from 'vue'
import { getMenuTreeOptionsApi } from '@/api/Admin/auth/menu'
import { Form, FormSchema } from '@/components/Form'
import { IconPicker } from '@/components/IconPicker'
import { useForm } from '@/hooks/web/useForm'
import { useValidator } from '@/hooks/web/useValidator'
import { useDictStore } from '@/store/Admin/dict'
import { propTypes } from '@/utils/propTypes'
import { DictDetail, trueFalseOptions } from '@/utils/dict'

const dictStore = useDictStore()

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  },
  parentId: propTypes.number.def(undefined)
})

const menuTypeOptions = ref<DictDetail[]>([])
const menuTreeOptions = ref([])

const getOptions = async () => {
  const dictOptions = await dictStore.getDictObj(['sys_dict_menu_type'])
  menuTypeOptions.value = dictOptions.sys_dict_menu_type

  const res = await getMenuTreeOptionsApi()
  menuTreeOptions.value = res.data
}

getOptions()

const formSchema = reactive<FormSchema[]>([
  {
    field: 'menu_type',
    label: '菜单类型',
    colProps: {
      span: 24
    },
    component: 'RadioButton',
    componentProps: {
      options: menuTypeOptions
    },
    value: '0'
  },
  {
    field: 'parent_id',
    label: '上级菜单',
    component: 'TreeSelect',
    componentProps: {
      checkStrictly: true,
      placeholder: '请选择上级菜单',
      nodeKey: 'value',
      defaultExpandAll: false
    },
    optionApi: async () => {
      return menuTreeOptions
    },
    value: props.parentId
  },
  {
    field: 'title',
    label: '菜单名称',
    component: 'Input'
  },
  {
    field: 'path',
    label: '路由地址',
    component: 'Input',
    colProps: {
      span: 12
    },
    ifshow: (formModel) => formModel.menu_type !== '2'
  },
  {
    field: 'component',
    label: '组件路径',
    component: 'Input',
    colProps: {
      span: 12
    },
    ifshow: (formModel) => formModel.menu_type !== '2'
  },
  {
    field: 'order',
    label: '显示排序',
    component: 'InputNumber',
    colProps: {
      span: 12
    },
    componentProps: {
      style: {
        width: '100%'
      }
    }
  },
  {
    field: 'redirect',
    label: '重定向',
    component: 'Input',
    colProps: {
      span: 12
    },
    ifshow: (formModel) => formModel.menu_type !== '2'
  },
  {
    field: 'icon',
    label: '菜单图标',
    component: 'Input',
    formItemProps: {
      style: {
        width: '300px'
      },
      slots: {
        default: (data) => {
          return (
            <>
              <div style="display: flex; justify-content: space-between">
                <IconPicker input-disabled={false} v-model={data['icon']} />
                <div style="margin-left: 10px"></div>
              </div>
            </>
          )
        }
      }
    },
    ifshow: (formModel) => formModel.menu_type !== '2'
  },
  {
    field: 'hidden',
    label: '是否隐藏',
    colProps: {
      span: 12
    },
    component: 'RadioGroup',
    componentProps: {
      style: {
        width: '100%'
      },
      options: trueFalseOptions
    },
    value: false,
    ifshow: (formModel) => formModel.menu_type !== '2'
  },
  {
    field: 'disabled',
    label: '是否禁用',
    colProps: {
      span: 12
    },
    component: 'RadioGroup',
    componentProps: {
      style: {
        width: '100%'
      },
      options: trueFalseOptions
    },
    value: false,
    ifshow: (formModel) => formModel.menu_type !== '2'
  },
  {
    field: 'perms',
    label: '权限标识',
    component: 'Input',
    colProps: {
      span: 12
    },
    ifshow: (formModel) => formModel.menu_type === '2'
  },
  {
    field: 'noCache',
    label: '禁用缓存',
    colProps: {
      span: 12
    },
    component: 'RadioGroup',
    componentProps: {
      style: {
        width: '100%'
      },
      options: trueFalseOptions
    },
    value: false,
    ifshow: (formModel) => formModel.menu_type === '1'
  }
])

const rules = reactive({
  title: [required()],
  menu_type: [required()],
  disabled: [required()],
  hidden: [required()],
  path: [required()],
  noCache: [required()],
  order: [required()]
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate()
  if (valid) {
    const formData = await getFormData()
    return formData
  }
}

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="formSchema" :labelWidth="100" />
</template>
